package com.itheima;

import java.util.ArrayList;

public class HomeWeek04 {
    public static void main(String[] args) {
        //创建5个学生对象存入ArrayList集合中
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三", 20, 100));
        students.add(new Student("李四", 18, 90));
        students.add(new Student("王五", 21, 70));
        students.add(new Student("赵六", 22, 50));
        students.add(new Student("李二", 20, 70));
        students.add(new Student("王蛋", 20, 20));
        System.out.println(getMax(students));
        getScore(students);
        showStudent(students);
    }

    //打印最高分的学员姓名、年龄、成绩  [要求封装1个方法  参数是集合对象   返回值类型为Student(有两个相同最高分返回一个学生数组)
    public static ArrayList<Student> getMax(ArrayList<Student> students) {
        int index = -1;
        int score = 0;
        ArrayList<Student> s = new ArrayList<>();
        for (int i = 0; i < students.size(); i++) {
            if (score < students.get(i).getScore()) {
                score = students.get(i).getScore();
                index = i;
            }
        }

        for(int j = 0; j < students.size(); j++) {
            if(j != index && students.get(j).getScore() == score){
                s.add(students.get(j));
            }
        }
        s.add(students.get(index));
        return s;
    }

    //打印5个学生的总成绩和平均分
    public static  void getScore(ArrayList<Student> students){
        int sum = 0;
        for (Student student : students) {
            sum +=student.getScore();
        }
        System.out.println("总成绩为："+sum);
        System.out.println("平均分为："+(double)sum/students.size());
    }

    //打印不及格的学员信息及数量
    public static void showStudent(ArrayList<Student> students){
        int count = 0;
        for (Student student : students) {
            if(student.getScore()<60){
                System.out.println(student.toString());
                count++;
            }
        }
        System.out.println("不及格的学生有"+count+"个");
    }
}
